package cn.tedu.tmall.front.account.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.validation.account.UserRules;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.account.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

@RestController
@RequestMapping("/users")
@Validated
@Api(tags = "1. 用户管理")
@Slf4j
public class UserController{
    @Autowired
    private IUserService userService;

    // TODO 用户注册

    @PostMapping("/password/update")
    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "原密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "query")
    })
    public JsonResult updatePassword(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                     @Pattern(regexp = UserRules.PATTERN_PASSWORD, message = UserRules.MESSAGE_PASSWORD_PATTERN) String oldPassword,
                                     @Pattern(regexp = UserRules.PATTERN_PASSWORD, message = UserRules.MESSAGE_PASSWORD_PATTERN) String newPassword){

        log.debug("开始处理修改密码的请求");
        userService.updatePassword(currentPrincipal, oldPassword, newPassword);
        return JsonResult.ok();

    }

    @PostMapping("/avatar/update")
    @ApiOperation("修改头像")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "avatar", value = "新头像的路径", required = true, paramType = "query")
    })
    public JsonResult updateAvatar(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                   @NotBlank(message = "请提交新头像的路径") String avatar){
        log.debug("开始处理修改头像");
        userService.updateAvatar(currentPrincipal,avatar);
        return JsonResult.ok();

    }

    @PostMapping("/phone/update")
    @ApiOperation("修改手机号码")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query")
    })
    public JsonResult updatePhone(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                  @Pattern(regexp = UserRules.PATTERN_PHONE, message = UserRules.MESSAGE_PHONE_PATTERN) String phone){

        log.debug("开始处理修改手机号");
        userService.updatePhone(currentPrincipal,phone);
        return JsonResult.ok();
    }

    @PostMapping("/email/update")
    @ApiOperation("修改电子邮箱")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "电子邮箱", required = true, paramType = "query")
    })
    public JsonResult updateEmail(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal,
                                  @Pattern(regexp = UserRules.PATTERN_EMAIL, message = UserRules.MESSAGE_EMAIL_PATTERN) String email){

        log.debug("开始处理修改邮箱");
        userService.updateEmail(currentPrincipal,email);
        return JsonResult.ok();
    }

    // TODO 查询当前用户基本信息
}
